﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Task;

namespace Backend.JYD.EFDAL.DAL
{
	public class TaskModelDAL : Backend.JYD.IDAL.Task.ITaskModel
	{
		#region 查询数据TaskModel

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<TaskModel> GetTaskModelByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
				IQueryable<TaskModel> query = db.TaskModels.Where(p=> p.Status > 0);
				//其他条件代码
				return new PageInfo<TaskModel>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public TaskModel GetTaskModelByTaskModeId(Int64 taskModeId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<TaskModel> query = db.TaskModels.Where(p => p.TaskModeId == taskModeId && p.Status > 0);
				return query.FirstOrDefault();
			}
		}

        public IList<TaskModel> GetTaskModelByTaskModeIds(IList<Int64> taskModeIds)
        {
            Int64[] ids = taskModeIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskModel> query = db.TaskModels.Where(p => ids.Contains(p.TaskModeId) && p.Status > 0);
                return query.ToList();
            }
        }

        public IList<TaskModel> GetAllTaskModels()
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskModel> query = db.TaskModels.Where(p=> p.Status > 0);
                return query.ToList();
            }
        }
		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据TaskModel
		/// </summary>
		public Int64 AddTaskModel(TaskModel info)
		{
			using (JydDataContext db = new JydDataContext())
			{
                db.TaskModels.Add(info);
				db.SaveChanges();
				return info.TaskModeId;
			}
		}
		#endregion

		#region 修改数据TaskModel

		/// <summary>
		/// 根据字段（TaskModeId）修改数据TaskModel
		/// </summary>
		public bool UpdateTaskModel(TaskModel info)
		{
			using (JydDataContext db = new JydDataContext())
			{
                var query = db.TaskModels.Where(p => p.TaskModeId == info.TaskModeId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
						//.......
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion

	}
}